﻿using System;
using System.Linq;
using System.Collections.Generic;
using Langben.DAL;
using Langben.BLL;
using Common;
using System.Web.Mvc;
using Langben.App.Models;
using Models;
using System.Data;
using App.Codes;
using App.Models;
namespace Langben.App.Controllers
{
    /// <summary>
    /// 菜单树形结构
    /// </summary>
    public class PersonTreeController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 获取树形页面的数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetTree(string isgeren)
        {
            List<PersonTree> listSystemTree = new List<PersonTree>();

            DataTable dt = new DataTable();

            if (isgeren == "1")
            {
                dt = new ClsCon().CreatDaTable("SELECT id,Name as text,ParentId,'0' as attributes,'' AS iconCls  FROM dbo.SysDepartment" +
               " UNION ALL SELECT Id,MyName as Name,SysDepartmentId AS ParentId,'0' as attributes,(CASE ISNULL(MobilePhoneNumber,'') WHEN '' THEN '' ELSE 'icon-ok' end ) AS iconCls FROM dbo.SysPerson"
               + " UNION ALL (SELECT '6990915C-14E9-4C25-A3D4-0E70A19FBA54' AS id, '个人通讯录' AS text,null AS ParentId,'0' AS attributes,'' AS iconCls) "
               + " UNION ALL (SELECT RowGuid AS id,Name AS text,'6990915C-14E9-4C25-A3D4-0E70A19FBA54' AS ParentId,'1' AS attributes,(CASE ISNULL(Tel,'') WHEN '' THEN '' ELSE 'icon-ok' end ) AS iconCls FROM dbo.TelList)");

            }
            else
            {
                dt =
                    new ClsCon().CreatDaTable(
                        "SELECT id,Name as text,ParentId,'0' as attributes,'' AS iconCls  FROM dbo.SysDepartment" +
                        " UNION ALL SELECT Id,MyName as Name,SysDepartmentId AS ParentId,'0' as attributes,'' AS iconCls FROM dbo.SysPerson");
            }
            PersonTreeNodeCollection tree = new PersonTreeNodeCollection();

            

            IList<PersonTree> trees = ModelConvertHelper<PersonTree>.ConvertToModel(dt);
           
            if (trees != null)
            {
                string parentId = Request["parentid"];//父节点编号
                if (string.IsNullOrWhiteSpace(parentId))
                {
                    tree.Bind(trees, null, ref listSystemTree);
                }
                else
                {
                    tree.Bind(trees, parentId, ref listSystemTree);
                }
            }           
            return Json(listSystemTree, JsonRequestBehavior.AllowGet);
        }
    }
    
}


